package com.pengcheng.taskapplication.net;

import android.util.Log;

import com.pengcheng.taskapplication.TaskApplication;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.util.SPUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

import okhttp3.CipherSuite;
import okhttp3.ConnectionSpec;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.TlsVersion;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;

/**
 * Created by kekex on 2019/2/28.
 * httpclient
 */

public class HttpClientV2 {

    //读超时长，单位：毫秒
    private static final int READ_TIME_OUT = 30000;
    //连接时长，单位：毫秒
    private static final int CONNECT_TIME_OUT = 30000;

    /**
     * 配置retrofit
     */
    public static Retrofit getRetrofit(String baseUrl) {
        ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                .tlsVersions(TlsVersion.TLS_1_1)
                .tlsVersions(TlsVersion.TLS_1_2)
                .cipherSuites(
                        CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
                        CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
                        CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
                .build();

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)
                .readTimeout(READ_TIME_OUT, TimeUnit.MILLISECONDS)
                .connectTimeout(CONNECT_TIME_OUT, TimeUnit.MILLISECONDS)
                .connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT))
                .protocols(Collections.singletonList(Protocol.HTTP_1_1))
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request original = chain.request();
                        HttpUrl url = original.url();
                        String path=url.encodedPath();
                        Request.Builder requestBuilder = original.newBuilder()
                        .addHeader("authorization",SPUtils.getString(TaskApplication.getInstance(),ConstantValue.SpType.newtoken));
                        Log.i("authorization",SPUtils.getString(TaskApplication.getInstance(),ConstantValue.SpType.newtoken));
                        Request request = requestBuilder.build();
                        return chain.proceed(request);
                    }
                })
                .build();


        Retrofit retrofit = new Retrofit.Builder()
                //.addConverterFactory(GsonConverterFactory.create(new Gson()))
                .addConverterFactory(CustomGsonConverterFactory.create())
               // .addConverterFactory(FastJsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(okHttpClient)
                .baseUrl(baseUrl)
                .build();

        return retrofit;
    }
    //拦截器
    public class RedirectInterceptor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request original = chain.request();
            Request.Builder requestBuilder = original.newBuilder()
                    .header("time", String.valueOf(System.currentTimeMillis()));
            Request request = requestBuilder.build();
            Response response=chain.proceed(request);
            return response;
        }
    }


}
